import { createRouter, createWebHashHistory } from "vue-router";
import { RouteRecordRaw } from 'vue-router';
import { isMobile } from '@/utils/common'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'home',
    redirect: '/pc/index'
  },
  // PC端-----------------------------------------------------------
  {
    path: '/pc',
    name: 'pc',
    component: () => import('../views/pc/index.vue'),
    children: [
      {
        // 首页
        path: '/pc/index',
        name: 'pc_index',
        component: () => import('../views/pc/home/index.vue')
      },
      {
        // 产品服务
        path: '/pc/product',
        name: 'pc_product',
        component: () => import('../views/pc/product/index.vue')
      },
      {
        // 解决方案
        path: '/pc/scheme',
        name: 'pc_scheme',
        component: () => import('../views/pc/scheme/index.vue')
      },
      {
        // 生态产品
        path: '/pc/ecology',
        name: 'pc_ecology',
        component: () => import('../views/pc/ecology/index.vue')
      },
      {
        // 生态产品
        path: '/pc/ecology/detail',
        name: 'pc_ecologyDetail',
        component: () => import('../views/pc/ecology/detail.vue')
      },
      {
        // 关于我们
        path: '/pc/about',
        name: 'pc_about',
        component: () => import('../views/pc/about/index.vue')
      },
      {
        // 新闻
        path: '/pc/news',
        name: 'pc_news',
        component: () => import('../views/pc/news/index.vue')
      },
      {
        // 联系我们
        path: '/pc/contact',
        name: 'pc_contact',
        component: () => import('../views/pc/contact/index.vue')
      }
    ]
  },
  // 移动端-----------------------------------------------------------
  {
    path: '/m',
    name: 'm',
    component: () => import('../views/mobile/index.vue'),
    children: [
      {
        // 首页
        path: '/m/index',
        name: 'm_index',
        component: () => import('../views/mobile/home/index.vue')
      },
      {
        // 产品服务
        path: '/m/product',
        name: 'm_product',
        component: () => import('../views/mobile/product/index.vue')
      },
      {
        // 解决方案
        path: '/m/scheme',
        name: 'm_scheme',
        component: () => import('../views/mobile/scheme/index.vue')
      },
      {
        // 生态产品
        path: '/m/ecology',
        name: 'm_ecology',
        component: () => import('../views/mobile/ecology/index.vue'),
      },
      {
        // 生态产品详情
        path: '/m/ecology/detail',
        name: 'm_ecology_detail',
        component: () => import('../views/mobile/ecology/ecologyDetail.vue')
      },
      {
        // 关于我们
        path: '/m/about',
        name: 'm_about',
        component: () => import('../views/mobile/about/index.vue')
      },
      {
        // 新闻
        path: '/m/news',
        name: 'm_news',
        component: () => import('../views/mobile/news/index.vue')
      },
      {
        // 招聘信息
        path: '/m/about/hire',
        name: 'm_about_hire',
        component: () => import('../views/mobile/about/hire.vue')
      },
      {
        // 联系我们
        path: '/m/contact',
        name: 'm_contact',
        component: () => import('../views/mobile/contact/index.vue')
      }
    ]
  },
  // 404页面
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404'
  }
] as RouteRecordRaw[];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  // 每次切换路由页面滚动到顶部
  scrollBehavior() { // to, from, savedPosition
    return { left: 0, top: 0 }
  }
})

const interceptList = ["/pc/product", "/pc/scheme", "/m/product", "/m/scheme"]; // 需要拦截的路由

// 全局前置守卫
router.beforeEach((to: any, from: any, next: any) => {
  console.log(to, from, next)
  if (interceptList.includes(to?.path)) {
    if (Object.keys(to?.query).length === 0) {
      next && next({ path: "/404" })
    }
  }
  const isMobileDevice = isMobile();
  if (isMobileDevice) {
    if (to?.path.startsWith('/pc')) {
      let path = to?.path.replace('/pc', '/m');
      next && next({ path, query: to?.query });
    } else {
      next && next();
    }
  } else {
    if (to?.path.startsWith('/m')) {
      let path = to?.path.replace('/m', '/pc');
      next && next({ path, query: to?.query });
    } else {
      next && next();
    }
  }
});

export default router
